﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.AutoMapper;
using Abp.Domain.Uow;
using Abp.Application.Services.Dto;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class ProductAppService : AbpZeroTemplateAppServiceBase, IProductAppService
    {
        private readonly IRepository<Product, int> productRepository;
        private readonly SubstanceManager substanceManager;

        public ProductAppService(IRepository<Product, int> _productRepository, SubstanceManager _substanceManager)
        {
            productRepository = _productRepository;
            substanceManager = _substanceManager;
        }


        [UnitOfWork(isTransactional: false)]
        public PagedResultOutput<Dto.GetProductOutPut> GetAll(string code, int MaxResultCount = 25, int SkipCount = 0)
        {
            List<Dto.GetProductOutPut> outPut = new List<Dto.GetProductOutPut>();
            var query = productRepository.GetAll();
            if (!string.IsNullOrWhiteSpace(code))
            {
                query = query.Where(x => x.Code.Contains(code));
            }


            var total = query.Count();
            var all = query.OrderBy(x => x.Code).ThenBy(x => x.Id).Skip(SkipCount).Take(MaxResultCount).ToList();
            foreach (var item in all)
            {
                var model = new Dto.GetProductOutPut() { Id = item.Id, Code = item.Code };
                if (item.SubstanceId > 0)
                {
                    var sub = substanceManager.Get(item.SubstanceId);
                    model.SubstanceId = item.SubstanceId;
                    model.SubstanceCAS = sub.CASNo;
                    model.SubstanceCnName = sub.CnName;
                    model.SubstanceEnName = sub.EnName;
                    model.SubstanceFormula = sub.MolecularFormula;
                    model.SubstanceHighRisk = "";
                    model.SubstanceMolecularWeight = sub.MolecularWeight;
                    model.SubstanceMolPngBase64 = sub.ImgFile;
                    model.SubstanceNote = "";
                }
                outPut.Add(model);
            }
            return new PagedResultOutput<Dto.GetProductOutPut>(total, outPut);

        }


        [UnitOfWork(isTransactional: false)]
        public async Task<Dto.GetProductOutPut> Get(int productId)
        {
            var product = await productRepository.GetAsync(productId);
            var model = product.MapTo<Dto.GetProductOutPut>();
            if (model.SubstanceId > 0)
            {
                var sub = substanceManager.Get(model.SubstanceId);
                model.SubstanceCAS = sub.CASNo;
                model.SubstanceCnName = sub.CnName;
                model.SubstanceEnName = sub.EnName;
                model.SubstanceFormula = sub.MolecularFormula;
                model.SubstanceHighRisk = "";
                model.SubstanceMolecularWeight = sub.MolecularWeight;
                model.SubstanceMolPngBase64 = sub.ImgFile;
                model.SubstanceNote = "";
            }
            return model;
        }
    }
}
